## Mathematica output file list:
#
@filenames = ("stCOMFn","deltastCOMFn","nstCOMFn","deltanstCOMFn","hipposFn","deltaHipposFn","nsslopeFn","deltaNSslopeFn","COMFn","deltaCOMFn","stCOMLenFn","nstCOMLenFn","stHipLenFn","nstHipLenFn","COMAngleFn","HipAngleFn","h1Fn","h2Fn","h3Fn","h4Fn");

## Parse the mathematica output files and write them to disk in a format usable by MATLAB:
#
for ($matnum = 0; $matnum < scalar(@filenames); $matnum++) {
    $filename = @filenames[$matnum];
    open(DAT, "< ./build_ntorso/$filename") || die("Could not open file: " . $filename);
    @raw_data = <DAT>;
    close(DAT);
    $mat_str = "function x@filenames[$matnum] = @filenames[$matnum](x)\n"; #remove p
    $mat_str .= "x@filenames[$matnum] = ";
     
    foreach $linevar (@raw_data) {
       $linevar =~ s/Subscript\[x, 1\]/x1/g;
       $linevar =~ s/Subscript\[x, 3\]/x3/g;
       $linevar =~ s/Subscript\[x, 4\]/x4/g;
       $linevar =~ s/Subscript\[x, 2\]/x2/g;

        $linevar =~ s/\},/\};/g;
        $linevar =~ s/\n/ ...\n/g;
        $linevar =~ s/Pi/pi/g;
        
         $linevar =~ s/Sqrt/sqrt/g;

	$linevar =~ s/Abs/abs/g;
	$linevar =~ s/Sign/sign/g;

        $linevar =~ s/Sec/sec/g;
        $linevar =~ s/Csc/csc/g;
        $linevar =~ s/Cot/cot/g;



        $linevar =~ s/ArcCos/acos/g;
        $linevar =~ s/Cos/cos/g;
        $linevar =~ s/Sin/sin/g;
        $linevar =~ s/Tan/tan/g;
        $linevar =~ s/Arctan/atan/g;

	$linevar =~ s/E/exp(1)/g;
        
        $linevar =~ s/\[/\(/g;
        $linevar =~ s/\]/\)/g;
        $linevar =~ s/\{/\[/g;
        $linevar =~ s/\}/\]/g;
        
        $mat_str .= $linevar;
    }
    $mat_str .= ";";

    $newfilename = @filenames[$matnum].".m";
    open(DAT, "> ./build_ntorso/$newfilename") || die "Can't open $newfilename : $!";
    print DAT $mat_str;
    close(DAT);
}
